﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Linq.Expressions;
using Sisocana.Core;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Dal.EntityModels;
using Sisocana.Dal;
using Sisocana.Dal.Interfaces;
using System.Data;


namespace Sisocana.Core
{
    public class RedSocialAsociadoService : ServiceBase<IRedSocialAsociadoModel>, IRedSocialAsociadoService
    {

        public RedSocialAsociadoService()
            : this(new RedSocialAsociadoRepository())
        {
        }

        private IRedSocialAsociadoRepository _RedSocialAsociadoRepository;
        public RedSocialAsociadoService(IRedSocialAsociadoRepository RedSocialAsociadoRepository)
        {
            _RedSocialAsociadoRepository = RedSocialAsociadoRepository ?? new RedSocialAsociadoRepository();
        }

        public void createRedSocialAsociadoMapperEF2Model()
        {
            AutoMapper.Mapper.CreateMap<RedSocialAsociado, RedSocialAsociadoModel>()
                .ForMember(s => s.RedesSocialesName, opt => opt.MapFrom(c => c.RedesSociales.nombre))
                .ForMember(s => s.idSitio, opt => opt.MapFrom(c => c.Sitio.id));
        }

        public void createRedSocialAsociadoMapperModel2EF() 
        {
            AutoMapper.Mapper.CreateMap<RedSocialAsociadoModel, RedSocialAsociado>()
                .ForMember(s => s.Sitio.id, opt => opt.MapFrom(c => c.idSitio));
                
        }



        public RedSocialAsociadoModel GetSingle(Expression<Func<RedSocialAsociadoModel, bool>> whereCondition)
        {
            
                      
            createRedSocialAsociadoMapperEF2Model();

            IList<RedSocialAsociado> rsEF = _RedSocialAsociadoRepository.GetAll();
            List<RedSocialAsociadoModel> rs = new List<RedSocialAsociadoModel>();

            rs = AutoMapper.Mapper.Map(rsEF, rs);

            IQueryable<RedSocialAsociadoModel> rsRet = rs.AsQueryable<RedSocialAsociadoModel>().Where(whereCondition);

            List<RedSocialAsociadoModel> ret = rsRet.ToList();

            if (ret.Count == 0)
                return null;

            return ret.First();
        }





        public int createRedSocialAsociado(RedSocialAsociadoModel reds)
        {

            RedSocialAsociado rsEF = new RedSocialAsociado();
            rsEF.cantVisualizar = reds.cantVisualizar;
            rsEF.tagBusqueda = reds.tagBusqueda;
            rsEF.tagUser = reds.tagUser;
            rsEF.width = reds.width;
            rsEF.heigth = reds.heigth;

            if (reds.BackgroundShellColor == null) reds.BackgroundShellColor = "#fffff";
            if (reds.ShellColor == null) reds.ShellColor = "#fffff";
            if (reds.BackgroundTweetsColor == null) reds.BackgroundTweetsColor = "#c0c0c0";
            if (reds.TweetsColor == null) reds.TweetsColor = "#00000";


            rsEF.BackgroundShellColor = reds.BackgroundShellColor;
            rsEF.ShellColor = reds.ShellColor;
            rsEF.BackgroundTweetsColor = reds.BackgroundTweetsColor;
            rsEF.TweetsColor = reds.TweetsColor;
            

            FuenteRepository _fuenteRepository = new FuenteRepository();
            IList<Fuente> fuentesEF = _fuenteRepository.GetAll();   
            IEnumerable<RedesSociales> listrssEF = fuentesEF.OfType<RedesSociales>();

            RedesSociales rssEF = listrssEF.AsQueryable<RedesSociales>().Where(c => c.nombre.Trim() == reds.RedesSocialesName.Trim()).First();
                    

            SiteRepository _SiteRepository = new SiteRepository();            
            Sitio sitEF = _SiteRepository.GetSingle(c => c.id == reds.idSitio);
            

            rsEF.Sitio = sitEF;
            rsEF.RedesSociales = rssEF;

            _RedSocialAsociadoRepository.Add(rsEF);

            try
            {
                _RedSocialAsociadoRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }


        public void Add(RedSocialAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(RedSocialAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public void Update(RedSocialAsociadoModel entity)
        {
            throw new NotImplementedException();
        }

        public IList<RedSocialAsociadoModel> GetAll(Expression<Func<RedSocialAsociadoModel, bool>> whereCondition)
        {
            createRedSocialAsociadoMapperEF2Model();

            IList<RedSocialAsociado> rsEF = _RedSocialAsociadoRepository.GetAll();
            List<RedSocialAsociadoModel> rs = new List<RedSocialAsociadoModel>();
            rs = AutoMapper.Mapper.Map(rsEF, rs);
            IQueryable<RedSocialAsociadoModel> rsRet = rs.AsQueryable<RedSocialAsociadoModel>().Where(whereCondition);            
            return rsRet.ToList();

        }

        IList<RedSocialAsociadoModel> IService<RedSocialAsociadoModel>.GetAll()
        {
            throw new NotImplementedException();
        }

        public IQueryable<RedSocialAsociadoModel> Query(Expression<Func<RedSocialAsociadoModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }

        public long Count(Expression<Func<RedSocialAsociadoModel, bool>> whereCondition)
        {
            throw new NotImplementedException();
        }


        public int updateRedSocialAsociado(RedSocialAsociadoModel reds)
        {
            //busco la asociacion 
            RedSocialAsociado rs = _RedSocialAsociadoRepository.GetSingle(c => c.id == reds.id);
            rs.tagBusqueda = reds.tagBusqueda;
            rs.tagUser = reds.tagUser;
            rs.cantVisualizar = reds.cantVisualizar;
            rs.width = reds.width;
            rs.heigth = reds.heigth;

            if (reds.BackgroundShellColor == null) reds.BackgroundShellColor = "#fffff";
            if (reds.ShellColor == null) reds.ShellColor = "#fffff";
            if (reds.BackgroundTweetsColor == null) reds.BackgroundTweetsColor = "#c0c0c0";
            if (reds.TweetsColor == null) reds.TweetsColor = "#00000";

            rs.BackgroundShellColor = reds.BackgroundShellColor;
            rs.ShellColor = reds.ShellColor;
            rs.BackgroundTweetsColor = reds.BackgroundTweetsColor;
            rs.TweetsColor = reds.TweetsColor;

            try
            {
                _RedSocialAsociadoRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }

        }


        public int deleteRedSocialAsociado(RedSocialAsociadoModel reds)
        {

            RedSocialAsociado rs = _RedSocialAsociadoRepository.GetSingle(c => c.id == reds.id);

            if (rs== null)
                return -2;

            try
            {
                _RedSocialAsociadoRepository.Delete(rs);
                _RedSocialAsociadoRepository.Save();
                return 0;
            }
            catch (Exception e)
            {
                return e.InnerException.GetHashCode();
            }
           
        }
    }
}
